﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace VERKProjectHopur16.Models
{
    public class DataRepository : IDataRepository
    {
        ContentDBDataContext m_db = new ContentDBDataContext();

        public IEnumerable<Content> GetAllContent()
        {
            var result = (from content in m_db.Contents
                          select content);
            return result;
        }

        public Content GetContentById(int id)
        {
            var result = (from content in m_db.Contents
                          where content.ContentID == id
                          select content).SingleOrDefault();
            return result;
        }

        public void UpdateContent(Content s)
        {
            Content t = GetContentById(s.ContentID);
            if (t != null)
            {
                t.InsertDate = DateTime.Now;
                t.Title = s.Title;
                t.Text = s.Text;
                t.CreatorID = s.CreatorID;
                t.DepartmentID = s.DepartmentID;
                t.CategoryID = s.CategoryID;
                t.AuthorID = s.AuthorID;
                t.LanguageID = s.LanguageID;
                m_db.SubmitChanges();
            }
        }

        public void AddContent(Content model)
        {
            m_db.Contents.InsertOnSubmit(model);
            m_db.SubmitChanges();
        }

        public void DeleteContent(Content model)
        {
            Content item = GetContentById(model.ContentID);
            if (item != null)
            {
                item.Active = false;
                m_db.SubmitChanges();
            }
        }

        public IEnumerable<Eventss> GetAllEvent()
        {
            var result = (from events in m_db.Eventsses
                          select events);
            return result;
        }

        public Eventss GetEventById(int id)
        {
            var result = (from events in m_db.Eventsses
                          where events.EventID == id
                          select events).SingleOrDefault();
            return result;
        }

        public void UpdateEvent(Eventss model)
        {
            Eventss item = GetEventById(model.EventID);
            if (item != null)
            {
                item.Name = model.Name;
                item.StartDate = model.StartDate;
                item.EndDate = model.EndDate;
                item.Description = model.Description;
                item.CreatorID = model.CreatorID;
                item.DepartmentID = model.DepartmentID;
                item.AuthorID = model.AuthorID;
                m_db.SubmitChanges();
            }
        }

        public void AddEvent(Eventss model)
        {
            m_db.Eventsses.InsertOnSubmit(model);
            m_db.SubmitChanges();
        }

        public void DeleteEvent(Eventss model)
        {
            Eventss item = GetEventById(model.EventID);
            if (item != null)
            {
                item.Active = false;
                m_db.SubmitChanges();
            }

        }

        public IEnumerable<QA> AllQA()
        {
            var result = (from Q_A in m_db.QAs
                          select Q_A);
            return result;
        }

        public QA GetQAById(int id)
        {
            var result = (from Q_A in m_db.QAs
                          where Q_A.QAID == id
                          select Q_A).SingleOrDefault();
            return result;
        }

        public void UpdateQA(QA model)
        {
            QA item = GetQAById(model.QAID);
            if (item != null)
            {
                item.Question = model.Question;
                item.Answer = model.Answer;
                item.InsertDate = item.InsertDate;
                item.CreatorEmail = model.CreatorEmail;
                item.AnswerID = model.AnswerID;
                item.Answered = model.Answered;
                item.DepartmentID = model.DepartmentID;
                item.Active = model.Active;
                m_db.SubmitChanges();
            }
        }

        public void UpdateQAAnswer(QA model)
        {
            QA item = GetQAById(model.QAID);
            if (item != null)
            {
                item.Answer = model.Answer;
                item.Answered = model.Answered;
                m_db.SubmitChanges();
            }
        }

        public void AddQA(QA model)
        {
            m_db.QAs.InsertOnSubmit(model);
            m_db.SubmitChanges();
        }

        public void DeleteQA(QA model)
        {
            QA item = GetQAById(model.QAID);
            if (item != null)
            {
                item.Active = false;
                m_db.SubmitChanges();
            }

        }

        public IEnumerable<Link> GetAllLink()
        {
            var result = (from links in m_db.Links
                          select links);
            return result;
        }

        public Link GetLinkById(int id)
        {
            var result = (from item in m_db.Links
                          where item.LinkID == id
                          select item).SingleOrDefault();
            return result;
        }

        public void UpdateLink(Link model)
        {
            Link item = GetLinkById(model.LinkID);
            if (item != null)
            {
                item.Url = model.Url;
                item.LinkTitle = model.LinkTitle;
                m_db.SubmitChanges();
            }
        }

        public void AddLink(Link model)
        {
            m_db.Links.InsertOnSubmit(model);
            m_db.SubmitChanges();
        }

        public void DeleteLink(Link model)
        {
            Link item = GetLinkById(model.LinkID);
            if (item != null)
            {
                item.Active = false;
                m_db.SubmitChanges();
            }
        }

        public IEnumerable<Department> GetAllDept()
        {
            var result = (from depts in m_db.Departments
                          select depts);
            return result;
        }

        public IEnumerable<Category> GetAllCateg()
        {
            var result = (from categ in m_db.Categories
                          select categ);
            return result;
        }

        public IEnumerable<Language> GetAllLang()
        {
            var result = (from item in m_db.Languages
                          select item);
            return result;
        }

        public IEnumerable<Author> GetAllAuthors()
        {
            var result = (from author in m_db.Authors
                          select author);
            return result;
        }

        public Author GetAuthorById(int id)
        {
            var result = (from author in m_db.Authors
                          where author.AuthorID == id
                          select author).SingleOrDefault();
            return result;
        }

        public void UpdateAuthor(Author model)
        {
            Author author = GetAuthorById(model.AuthorID);
            if (author != null)
            {
                author.Name = model.Name;
                author.Email = model.Email;
                m_db.SubmitChanges();
            }
        }

        public void AddAuthor(Author model)
        {
            m_db.Authors.InsertOnSubmit(model);
            m_db.SubmitChanges();
        }

        public void DeleteAuthor(Author model)
        {
            Author item = GetAuthorById(model.AuthorID);
            if (item != null)
            {
                item.Active = false;
                m_db.SubmitChanges();
            }
        }

        public IEnumerable<Child> AllChilds()
        {
            var model = from item in m_db.Childs
                        select item;
            return model;
        }

        public Child GetChildById(int id)
        {
            var model = (from item in m_db.Childs
                         where item.ChildID == id
                         select item).SingleOrDefault();
            return model;
        }

        public void AddChild(Child model)
        {
            m_db.Childs.InsertOnSubmit(model);
            m_db.SubmitChanges();
        }

        public void DeleteChild(Child model)
        {
            Child item = GetChildById(model.ChildID);
            if (item != null)
            {
                item.Active = false;
                m_db.SubmitChanges();
            }

        }        
        
        public IEnumerable<Picture> AllPictures()
        {
            var result = (from picture in m_db.Pictures
                          select picture);
            return result;
        }

        public void AddPicture(Picture model)
        {
            m_db.Pictures.InsertOnSubmit(model);
            m_db.SubmitChanges();
        }

        public Picture GetPictureById(int id)
        {
            var item = (from picture in m_db.Pictures
                        where picture.PictureID == id
                        select picture).SingleOrDefault();

            return item;
        }

        public void UpdatePicture(Picture model)
        {
            Picture pic = GetPictureById(model.PictureID);
            if (pic != null)
            {
                pic.Name = model.Name;
                pic.PictureText = model.PictureText;
                m_db.SubmitChanges();
            }
        }

        public IEnumerable<aspnet_Membership> AllUsers()
        {
            var model = from item in m_db.aspnet_Memberships
                        select item;
            return model;
        }

        public aspnet_User GetUserById(string id)
        {
            Guid GuidID = new Guid(id);
            var model = (from item in m_db.aspnet_Users
                         where item.UserId == GuidID
                         select item).SingleOrDefault();
            return model;
        }

        public IEnumerable<aspnet_UsersInRole> AllRoles()
        {
            var model = from item in m_db.aspnet_UsersInRoles
                        select item;
            return model;
        }
    }
}
